{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1,循环语句，for，while"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for a in [3, 4, 5, 6, 'life']:\n",
    "    print(a)\n",
    "\n",
    "for idx, a in enumerate([3, 4, 5, 6, 'life']):\n",
    "    print(idx, a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10\n"
     ]
    }
   ],
   "source": [
    "#print(range(5))\n",
    "sum = 0\n",
    "for i in range(5):#0,1,2,3,4\n",
    "    sum += i\n",
    "    #sum = sum + i\n",
    "print(sum)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5050\n"
     ]
    }
   ],
   "source": [
    "i = 1\n",
    "sum = 0\n",
    "while i <= 100:\n",
    "    sum += i\n",
    "    i += 1\n",
    "print(sum)\n",
    "#while True:\n",
    "#    break\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "continue,break语句"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "6\n",
      "7\n",
      "8\n",
      "9\n"
     ]
    }
   ],
   "source": [
    "for a in range(10):\n",
    "    print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "4\n",
      "5\n",
      "6\n",
      "7\n",
      "8\n",
      "9\n"
     ]
    }
   ],
   "source": [
    "for b in range(3, 10):\n",
    "    print(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "5\n",
      "7\n",
      "9\n"
     ]
    }
   ],
   "source": [
    "for c in range(3, 10, 2):\n",
    "    print(c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10\n",
      "9\n",
      "8\n",
      "7\n",
      "6\n",
      "5\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "for d in range(10, 3, -1):\n",
    "    print(d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in range(1,11):\n",
    "    if i%3 == 0:\n",
    "        print('{}能被3整除'.format(i))\n",
    "        continue\n",
    "    print('{}不能被3整除'.format(i)) \n",
    "\n",
    "sum = 0\n",
    "for i in range(1,101):\n",
    "    if sum < 1000 and sum +i > 1000:\n",
    "        print(i-1, sum)\n",
    "        break\n",
    "    else:\n",
    "        sum += i\n",
    "    #sum += i\n",
    "    # if sum > 1000:\n",
    "    #     print(i-1, sum-(i))\n",
    "    #     break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in range(1, 10, 2):\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "could not convert string to float: 'sbdderer'\n",
      "您输入的内容不符合要求\n",
      "could not convert string to float: 'sds'\n",
      "您输入的内容不符合要求\n",
      "could not convert string to float: 'ddf'\n",
      "您输入的内容不符合要求\n",
      "3 3.0\n"
     ]
    }
   ],
   "source": [
    "#用try和except判断输入的是一个正数，如果不是正数就一直循环直到输入正数\n",
    "\n",
    "while True:\n",
    "    str = input(\"请输入大于0的正数\")\n",
    "    try:\n",
    "        x = float(str)\n",
    "        if x <= 0:\n",
    "            print('你输入的数小于等于0,请输入大于0的正数')\n",
    "        else:\n",
    "            print(str, x)\n",
    "            break\n",
    "    except Exception as e:\n",
    "        print(e)\n",
    "        print('您输入的内容不符合要求')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.9.12 ('base')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "ad2bdc8ecc057115af97d19610ffacc2b4e99fae6737bb82f5d7fb13d2f2c186"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
